<template>
  <view class="layout">
    <view class="caption">
      <view class="text">猜你喜欢</view>
    </view>

    <view class="guess">
      <navigator
        :url="'/pages/goods/goods?id=' + item.id"
        class="guess-item"
        v-for="(item, index) in guessList"
        :key="index"
      >
        <image :src="item.picture" mode="aspectFill"></image>
        <view class="name">{{ item.name }}</view>
        <view class="price">
          <text class="small">¥</text>
          <text>{{ item.price }}</text>
        </view>
      </navigator>
    </view>
    <view class="laoding" v-if="finish">正在加载... </view>
  </view>
</template>

<script setup lang="ts">
import { getGuessAPI } from '@/services/home'
import type { PageParams } from '@/types/global'
import type { GuessItem, HomeItem } from '@/types/home.d.ts'
import { onMounted, ref } from 'vue'

// 分页参数 Required<T> 类型为？的参数是可选 加则个是可选都变成了必填
const pageparams: Required<PageParams> = {
  page: 1,
  pageSize: 10,
}

// ([]) 这里面是默认值
const guessList = ref<GuessItem[]>([])
const finish = ref(false)

// 通过api获取数据
const getHomeGoodsGuessLikeData = async () => {
  // 是否继续执行
  if (finish.value === true) {
    uni.showToast({ icon: 'none', title: '没有更多数据~' })
  }

  // 获取数据
  let res = await getGuessAPI(pageparams)
  // 数据累计
  // guessList.value =  [...res.result.items,...guessList.value!]
  guessList.value.push(...res.result.items)
  console.log(res)
  // 增加页码
  //如果没有达到最大页码就可以加
  if (res.result.pages > pageparams.page) {
    pageparams.page++
  } else {
    // 展示已经加载完
    finish.value = true
  }
}

// 重置数据
const resetData = () => {
  pageparams.page = 1
  guessList.value = []
  finish.value = false
}

// 组件挂载完毕
onMounted(() => {
  getHomeGoodsGuessLikeData()
})
// 父调子 这里是子暴露出去
defineExpose({
  resetData,
  getMore: getHomeGoodsGuessLikeData,
})
</script>

<style lang="scss" scoped>
.layout {
  .caption {
    display: flex;
    justify-content: center;
    padding: 36rpx 0 40rpx;
    .text {
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 0 28rpx 0 30rpx;

      &::before,
      &::after {
        content: '';
        width: 20rpx;
        height: 20rpx;
        background-image: url(@/static/images/bubble.png);
        background-size: contain;
        margin: 0 10rpx;
      }
    }
  }
  .guess {
    display: flex;
    flex-wrap: wrap;
    padding: 0 20rpx;
    .guess-item {
      padding: 24rpx 20rpx 20rpx;
      width: 345rpx;
      border-radius: 10rpx;
      overflow: hidden;
      background-color: #fff;
      margin-bottom: 20rpx;
      image {
        width: 300rpx;
        height: 300rpx;
      }
      .name {
        height: 75rpx;
        font-size: 26rpx;
        line-height: 1.2rem;
        margin: 10rpx 0;
        color: #262626;
        overflow: hidden;
        //待理解
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        text-overflow: ellipsis;
        // white-space: nowrap;
      }
      .price {
        padding-top: 4rpx;
        color: #cf4444;
        font-size: 26rpx;
        .small {
          line-height: 1.5em;
          font-size: 15rpx;
          margin-right: 5rpx;
        }
      }
    }
  }
  .laoding {
    text-align: center;
    font-size: 28rpx;
    color: #666;
    padding: 20rpx 0;
  }
}
</style>
